package cc.autoapi.pucong.auto.flow.persistence.repository.api;


import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.api.design.ApiNodeParamsVo;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.api.design.ApiNodeRelationVo;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.api.design.ApiNodeVo;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.TaskNode;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.TaskNodeParamIn;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.TaskNodeParamOut;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.entity.node.TaskNodeRelation;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.exec.DataBus;
import cc.autoapi.pucong.auto.flow.core.flow.execNode.utils.FlowDesignUtils;
import cc.autoapi.pucong.auto.flow.core.root.exception.AutoFlowException;
import cc.autoapi.pucong.auto.flow.core.root.selfenum.ApiTypeEnum;
import cc.autoapi.pucong.auto.flow.core.root.selfenum.CommonBoolenEnum;
import cc.autoapi.pucong.auto.flow.persistence.utils.jdbc.DatabaseUtils;
import cc.autoapi.pucong.auto.flow.persistence.po.api.ApiInfoPo;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @author pucong
 * @description 接口信息 service 实现类
 * @create 2024-09-24
 */
@Service
public class ApiInfoServiceImpl implements ApiInfoService {

    @Resource
    private ApiNodeService apiNodeService;

    @Override
    public void initPublishApi() {
        List<ApiInfoPo> apiInfoPos = selectList(CommonBoolenEnum.是);
        for (ApiInfoPo apiInfoPo : apiInfoPos) {
            String apiId = apiInfoPo.getUuid();
            publishApi(apiId);
        }
    }

    private List<ApiInfoPo> selectList(CommonBoolenEnum commonBoolenEnum) {
        List<Object> params = new ArrayList<>();
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select * from auto_flow_api_info where 1=1 and api_status = ? order by api_name asc ");
        params.add(commonBoolenEnum.getValue());
        List<ApiInfoPo> apiInfoPos = DatabaseUtils.execQueryListSql(params, sqlBf.toString(), ApiInfoPo.class);
        return apiInfoPos;
    }

    @Override
    public void publishApi(String apiId) {
        ApiInfoPo byId = getById(apiId);
        if (byId == null) {
            throw new AutoFlowException("传入参数错误");
        }
        List<ApiNodeRelationVo> addRelateList = new ArrayList<>();
        List<ApiNodeVo> addNodeList = new ArrayList<>();
        List<ApiNodeParamsVo> apiNodeParamsVoList = new ArrayList<>();
        // 解析json
        FlowDesignUtils.dealJson2List(byId.getApiJson(),
                addNodeList, addRelateList,
                apiNodeParamsVoList);
        List<TaskNode> taskNodeList = apiNodeService.getTaskNodeList(addNodeList, apiNodeParamsVoList);
        List<TaskNodeRelation> taskNodeRelationList = apiNodeService.getTaskNodeRelationList(addRelateList);

        DataBus.setTaskNodeList(apiId, taskNodeList);
        DataBus.setTaskNodeRelationList(apiId, taskNodeRelationList);
        List<TaskNodeParamOut> taskNodeParamOutList = new ArrayList<>();
        List<TaskNodeParamIn> taskNodeParamInList = new ArrayList<>();
        for (TaskNode execNode : taskNodeList) {
            taskNodeParamOutList.addAll(execNode.getTaskNodeParamOutList());
            taskNodeParamInList.addAll(execNode.getTaskNodeParamInList());
        }
        DataBus.setExeNodeParamsOutList(apiId, taskNodeParamOutList);
        DataBus.setExeNodeParamsInList(apiId, taskNodeParamInList);
    }

    @Override
    public void removeApi(String apiId) {
        DataBus.removeTaskNodeList(apiId);
        DataBus.removeTaskNodeRelationList(apiId);
        DataBus.removeTaskNodeParamsInList(apiId);
        DataBus.removeTaskNodeParamsOutList(apiId);
    }

    @Override
    public ApiInfoPo getOneByCodesAndType(String[] strings, ApiTypeEnum requestMethod) {
        List<Object> params = new ArrayList<>();
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select * from auto_flow_api_info where 1=1 and api_code in (?,?) and api_type = ?");
        params.addAll(Arrays.asList(strings));
        params.add(requestMethod.getValue());
        ApiInfoPo apiInfoPo = DatabaseUtils.execQueryOneSql(params, sqlBf.toString(), ApiInfoPo.class);
        return apiInfoPo;
    }

    @Override
    public ApiInfoPo getById(String apiId) {
        List<Object> params = new ArrayList<>();
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select * from auto_flow_api_info where 1=1 and uuid = ?");
        params.add(apiId);
        ApiInfoPo apiInfoPo = DatabaseUtils.execQueryOneSql(params, sqlBf.toString(), ApiInfoPo.class);
        return apiInfoPo;
    }

    @Override
    public List<ApiInfoPo> list(String apiName, String groupId, CommonBoolenEnum apiStatus) {
        List<Object> params = new ArrayList<>();
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select * from auto_flow_api_info where 1=1 ");
        if (StringUtils.isNotBlank(apiName)) {
            sqlBf.append(" and api_name like '%?%' ");
            params.add(apiName);
        }
        if (StringUtils.isNotBlank(groupId)) {
            sqlBf.append(" and group_id = ? ");
            params.add(groupId);
        }
        if (apiStatus != null) {
            sqlBf.append(" and api_status = ? ");
            params.add(apiStatus.getValue());
        }
        sqlBf.append(" order by api_name asc ");

        List<ApiInfoPo> apiInfoPos = DatabaseUtils.execQueryListSql(params, sqlBf.toString(), ApiInfoPo.class);
        return apiInfoPos;
    }

    @Override
    public Boolean updateById(ApiInfoPo apiInfoUpdate) {
        Integer i = DatabaseUtils.updateAutoById("auto_flow_api_info", apiInfoUpdate);
        return true;
    }

    @Override
    public Boolean save(ApiInfoPo apiInfo) {
        Integer i = DatabaseUtils.saveAuto("auto_flow_api_info", apiInfo);
        return true;
    }

    @Override
    public Boolean removeBatchByIds(List<String> ids) {
        if (ids == null || ids.size() < 1) {
            return false;
        }
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("delete from auto_flow_api_info where uuid in (");
        for (int i = 0; i < ids.size(); i++) {
            sqlBf.append("?,");
        }
        sqlBf.deleteCharAt(sqlBf.length() - 1);
        sqlBf.append(")");
        List<Object> params = new ArrayList<>();
        params.addAll(ids);

        DatabaseUtils.execSql(params, sqlBf.toString(), Integer.class);
        return true;
    }
}
